package com.vm.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.vm.commons.VMConst;
import com.vm.model.ActiveSoft;
import com.vm.model.Soft;
import com.vm.myview.ViewUtils;
import com.vm.utils.JsonUtils;
import com.wwt.app.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年1月29日 下午5:13:15]
 * @Version: [v1.0]
 * 
 */
public class CrackGameActivity extends Activity {

	protected static final String TAG = "CrackGameActivity";
	private RequestQueue mQueue;
	ImageListener listener;
	ImageLoader mImageLoader;
	private List<Soft> listsofts;
	ViewUtils viewUtils;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.crack_game);

		mQueue = Volley.newRequestQueue(this);
		viewUtils = new ViewUtils(this);

		listsofts = new ArrayList<Soft>();
		String url = VMConst.REQUEST_URL + "getCrackGame";
		
		mQueue = Volley.newRequestQueue(this);

		JsonArrayRequest json_EP = new JsonArrayRequest(
				url, new Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray arg0) {

						listsofts.clear();
						for (int i = 0; i < arg0.length(); i++) {
							try {
								JSONObject jsonObject = arg0.getJSONObject(i);
								final ActiveSoft activeSoft = JsonUtils.jsonToBean(
										jsonObject, ActiveSoft.class);

								LinearLayout found_ll_1 = (LinearLayout) findViewById(R.id.found_ll_1);

								found_ll_1.addView(viewUtils.dyAddView(i, activeSoft,1));

							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

					}

				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						// Log.e(TAG, error.getMessage(), error);
					}
				});

		mQueue.add(json_EP);

	}
	
//	/**
//	 * 动态创建LinearLayout
//	 * 
//	 * @param i
//	 * @param activeSoft
//	 * @return
//	 */
//	private LinearLayout dyAddView(int i, final ActiveSoft activeSoft) {
//
//		LinearLayout linearLayout = (LinearLayout) CrackGameActivity.this
//				.getLayoutInflater().inflate(R.layout.found_item, null);
//		if (i == 0) {
//			mImageLoader = new ImageLoader(mQueue, new BitmapCache());
//		}
//
//		TextView tv_softName = (TextView) linearLayout
//				.findViewById(R.id.tv_softName);
//		ImageView iv_softImage = (ImageView) linearLayout
//				.findViewById(R.id.iv_softImage);
//
//		if (activeSoft.getName() != null) {
//			// String softName =
//			// StringUtil.getShotName(soft.getName());
//			tv_softName.setText(activeSoft.getName());
//		}
//
//		listener = ImageLoader
//				.getImageListener(iv_softImage,
//						android.R.drawable.ic_menu_rotate,
//						android.R.drawable.ic_delete);
//
//		mImageLoader.get(VMConst.URL + activeSoft.getImageUrl(), listener);
//
//		OnClickListener ll_listener = new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent(CrackGameActivity.this,
//						DetailActivity.class);
//
//				intent.putExtra("softId", activeSoft.getSoftId());
//				startActivityForResult(intent, 10);
//			}
//		};
//
//		linearLayout.setOnClickListener(ll_listener);
//
//		return linearLayout;
//	}


//	OnItemClickListener lv_listener = new OnItemClickListener() {
//
//		@Override
//		public void onItemClick(AdapterView<?> parent, View view, int position,
//				long id) {
//
//			Intent intent = new Intent(CrackGameActivity.this,
//					DetailActivity.class);
//			TextView textView = (TextView) view.findViewById(R.id.tv_softName);
//			String softId = textView.getTag().toString();
//			intent.putExtra("softId", softId);
//			startActivityForResult(intent, 10);
//
//		}
//	};
}
